﻿using FastDev;
using UnityEngine;

public class CollectWood : GoapAction
{
    public override string Name { get; protected set; } = "收集木头";
    public override int Cost { get; protected set; } = 5;
    public override GoapState PreCondition { get; protected set; } = new GoapState(new System.Collections.Generic.Dictionary<string, int>() {
        {AIStateKey.HP,50},
    });
    public override GoapState Effect { get; protected set; } = new GoapState(new System.Collections.Generic.Dictionary<string, int>() {
        {AIStateKey.Wood,200},
    });
    public override GoapAgent Agent { get; protected set; }

    public GameObject Target { get; protected set; } = GameObject.FindGameObjectWithTag("Wood");

    public CollectWood(GoapAgent goapAgent) : base(goapAgent) { }

    public override void OnStart()
    {
        base.OnStart();
        count = 0;
        Progress = 0;
    }

    private float acTime = 2;
    private float count = 0;
    public override void OnUpdate()
    {
        base.OnUpdate();

        if (Vector3.Distance(Target.transform.position, Agent.transform.position) > 1)
        {
            Agent.transform.Translate((Target.transform.position - Agent.transform.position).normalized * 10 * Time.deltaTime);
            return;
        }

        count += Time.deltaTime;
        Progress = count / acTime;
        if (Progress >= 1)
        {
            Agent.GoapState.SetValue(AIStateKey.HP, Agent.GoapState.GetValue(AIStateKey.HP)-50);
            Agent.GoapState.SetValue(AIStateKey.Wood, 200);
        }
    }
}